<template>
    <bpmnModule @save="save" :xml="xml" :users="users" :groups="groups" :fromList="fromList" @dataType="dataType">
    </bpmnModule>
</template>

<script>
import { readXml, roleList, saveXml, userList } from "../../components/process/api/definition";
import bpmnModule from "../../components/process/index.vue"
import { listForm } from "../../components/process/api/form"
export default {
    components: {
        bpmnModule,
    },
    data() {
        return {
            xml: "",
            modeler: "",
            xmlOpen: false,
            xmlTitle: '',
            xmlContent: '',
            users: [],
            groups: [],
            fromList: [],
        }
    },
    created() {
        const deployId = this.$route.query && this.$route.query.deployId;
        //  查询流程xml
        if (deployId) {
            this.getModelDetail(deployId);
        }
        this.getDataList();
    },
    methods: {
        getDataList() {
            this.users = [];
            this.groups = [];
            userList().then(res => {
                res.data.forEach(val => {
                    val.userId = val.userId.toString();
                })
                this.users = res.data;
                let arr = { nickName: "流程发起人", userId: "${INITIATOR}" }
                this.users.push(arr)
            });
            roleList().then(res => {
                res.data.forEach(val => {
                    val.roleId = val.roleId.toString();
                })
                this.groups = res.data;
            });
            listForm({
                pageNum: 1,
                pageSize: 10,
                formName: null,
                formContent: null
            }).then(res => {
                this.fromList = res.rows;
            })
        },
        save(data) {
            const params = {
                name: data.process.name,
                category: data.process.category,
                xml: data.xml
            }
            saveXml(params).then(res => {
                // 关闭当前标签页并返回上个页面
                this.$message.success('添加成功');
                this.$router.go(-1)
            })
        },
        dataType(data) {
            this.users = [];
            this.groups = [];
            if (data) {
                if (data.dataType === 'dynamic') {
                    if (data.userType === 'assignee') {
                        this.users = [{ nickName: "${INITIATOR}", userId: "${INITIATOR}" },
                        { nickName: "#{approval}", userId: "#{approval}" }
                        ]
                    } else if (data.userType === 'candidateUsers') {
                        this.users = [{ nickName: "#{approval}", userId: "#{approval}" }]
                    } else {
                        this.groups = [{ roleName: "#{approval}", roleId: "#{approval}" }]
                    }
                } else {
                    this.getDataList()
                }
            }

        },
        getModelDetail(deployId) {
            // 发送请求，获取xml
            readXml(deployId).then(res => {
                this.xml = res.data;
                this.modeler = res.data
            })
        },
    }
}
</script>

<style>

</style>